package cn.dnui_zll731.dnuifood_731zlll.Adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import cn.dnui_zll731.dnuifood_731zlll.R;
import cn.dnui_zll731.dnuifood_731zlll.bean.CommentsBean;
import cn.dnui_zll731.dnuifood_731zlll.bean.Result;
import cn.dnui_zll731.dnuifood_731zlll.common.Constants;
import cn.dnui_zll731.dnuifood_731zlll.listener.RetrofitListener;
import cn.dnui_zll731.dnuifood_731zlll.model.UserModel;

public class PingjiaAdapter extends RecyclerViewAdapter<PingjiaAdapter.ViewHolder> implements RetrofitListener<Result> {
    private UserModel userModel;
    private int pos;

    public PingjiaAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final CommentsBean entity = (CommentsBean)data.get(position);
        holder.time.setText(entity.getOrder_time());
        holder.foodname.setText(entity.getFoodname());
        holder.shopname.setText("【"+entity.getShopname()+"】");
        holder.price.setText("￥"+entity.getPrice());
        holder.pingjia.setText("评价："+entity.getContent());

        holder.btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText editText = new EditText(context);
                editText.setText(entity.getContent());
                new AlertDialog.Builder(context).setTitle("修改评论")
                        .setView(editText)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                upPingjia(entity.getItem_id(),editText.getText().toString());//修改评价
                            }
                        }).setNegativeButton("取消",null)
                        .show();
            }
        });
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pos=position;
                deletepingjia(entity.getItem_id());
            }
        });

    }
    private void deletepingjia(String item_id){
        userModel = new UserModel();
        userModel.deleteComment(item_id,this);
    }
    private void upPingjia(String item_id, String ed){//修改评价
        userModel = new UserModel();
        userModel.updateComment(item_id,ed,this);
    }
    @Override
    public void onSuccess(Result object, int flag) {
        switch (flag){
            case Constants.UPDATECOMMENT:
                if (object.getSuccess().equals("1")){
                    Toast.makeText(context,"评论修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(context,"评论修改失败",Toast.LENGTH_SHORT).show();
                }

                break;
            case Constants.DELETECOMMENT:
                if (object.getSuccess().equals("1")){
                    data.remove(pos);
                    notifyDataSetChanged();//强制调用getView（）刷新item内容
                    Toast.makeText(context,"评论删除成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(context,"评论删除失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onFalure() {
        Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView time,foodname,price,shopname,pingjia;
        private Button btn_update,btn_delete;

        public ViewHolder(View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.pj_time);
            foodname = itemView.findViewById(R.id.pj_foodname);
            price = itemView.findViewById(R.id.pj_jiage);
            shopname = itemView.findViewById(R.id.pj_shopname);
            btn_update = itemView.findViewById(R.id.pj_btn_xiygai);
            btn_delete = itemView.findViewById(R.id.pj_btn_shanchu);
            pingjia  = itemView.findViewById(R.id.pj_pingjia);
        }
    }
}
